package com.zsc.projectmanagementapi.service.Impl;

import com.zsc.projectmanagementapi.commons.ServerResponse;
import com.zsc.projectmanagementapi.dao.AnalysisMapper;
import com.zsc.projectmanagementapi.pojo.VO.Completion;
import com.zsc.projectmanagementapi.pojo.VO.ProjectIncome;
import com.zsc.projectmanagementapi.pojo.VO.Series;
import com.zsc.projectmanagementapi.pojo.VO.UnFinishProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Bernie_fang
 * @Description:
 * @create 2021/6/19 16:36
 **/
@Service
public class AnalysisServiceImpl {

    @Autowired
    private AnalysisMapper analysisMapper;

    public ServerResponse getAllUnFinishProject() {
        List<UnFinishProject> allUnFinishProject = analysisMapper.getAllUnFinishProject();
        return ServerResponse.createBySuccess(allUnFinishProject);
    }

    public ServerResponse getProjectIncome() {

        List<ProjectIncome> projectIncomes = analysisMapper.getProjectIncome();
        double total = 0;
        for (ProjectIncome income : projectIncomes) {
            total += income.getIncome();
        }
        for (ProjectIncome income : projectIncomes) {
            income.setPercentage(((double) income.getIncome() / total) * 100);
        }
        return ServerResponse.createBySuccess(projectIncomes);

    }

    public ServerResponse postProjectIncome(String ids) {
        String[] split = ids.split(",");
        List<ProjectIncome> incomeList = new ArrayList<>();
        double total = 0;
        for (String s : split) {
            ProjectIncome income = analysisMapper.postProjectIncome(s);
            total += income.getIncome();
            incomeList.add(income);
        }
        for (ProjectIncome income : incomeList) {
            income.setPercentage(((double) income.getIncome() / total) * 100);
        }
        return ServerResponse.createBySuccess(incomeList);
    }

    public ServerResponse getCompletion() {
        List<Completion> completions = analysisMapper.getCompletion();
        List<String> projectNames = new ArrayList<>();
        List<Series> seriess = new ArrayList<>();
        // List<Integer> data = new ArrayList<>();
        for (int i = 0; i < completions.size(); i++) {
            Completion completion = completions.get(i);
            System.out.println(completion);

            projectNames.add(completion.getProjectName());
            Series series = new Series();
            series.setName(completion.getProjectName());
            double data = (completion.getState() + 1) / 4.0;
            System.out.println(data);
            series.setData(data * 100);

            // int jk = 0;
            // for (jk = 0; jk < completions.size(); jk++);
            // {
            //     if (jk == i){
            //         data.add((completion.getState() + 1) / 4);
            //     }else {
            //         data.add(0);
            //     }
            // }
            seriess.add(series);
        }
        // System.out.println(projectNames);
        // Map<String,Object> map = new HashMap<>();
        // map.put("projectNames",projectNames);
        // map.put("seriess",seriess);
        return ServerResponse.createBySuccess(seriess);
    }
}
